package com.blockmeta.bbs.businesslibrary.activity;

import android.content.Context;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.blockmeta.bbs.baselibrary.net.callback.Callback;
import com.blockmeta.bbs.baselibrary.utils.loading.ProgressBarUtil;
import com.blockmeta.bbs.businesslibrary.R;
import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.net.okhttp.AdapterLoadDataBusiness;
import com.blockmeta.bbs.businesslibrary.net.okhttp.OKHttpGetReq;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;

/**
 * Created by author on 2018/5/11.
 */

public abstract class BaseListActivity<T> extends BaseActivity implements OnLoadmoreListener, OnRefreshListener {
    public Context mContext;
    private View view;
    public SmartRefreshLayout list_swipe;
    public RecyclerView list_recy;
    private FrameLayout header_container;
    public LinearLayout mContainer;

    @Override
    protected boolean showTitle() {
        return showTitleBar();
    }

    public abstract boolean showTitleBar();

    @Override
    public View setView() {
        mContext = BaseListActivity.this;
        initBundle();
        view = LayoutInflater.from(mContext).inflate(R.layout.fragment_list_smart_header, null);
        list_swipe = (SmartRefreshLayout) view.findViewById(R.id.list_swipe);
        list_recy = (RecyclerView) view.findViewById(R.id.list_recy);
        header_container = (FrameLayout) view.findViewById(R.id.header_framelayout);
        mContainer = (LinearLayout) view.findViewById(R.id.ll_container);
        list_recy.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        list_swipe.setOnLoadmoreListener(this);
        list_swipe.setOnRefreshListener(this);
        list_swipe.setRefreshFooter(new ClassicsFooter(mContext));
        if (addCHooseHeaderView() == null) {
            header_container.setVisibility(View.GONE);
        } else {
            header_container.addView(addCHooseHeaderView());
        }
        initNet();
        return view;
    }

    protected abstract View addCHooseHeaderView();

    protected abstract void initBundle();


    public T mPOJO;
    public OKHttpGetReq mGetReq;


    /**
     * 设置分页参数
     */
    public String pageNo;
    public BaseQuickAdapter mAdapter;

    public String getPageNo() {
        if (pageNo == null) {
            pageNo = "";
        }
        return pageNo;
    }

    public void setPageNo(String pageNo) {
        this.pageNo = pageNo;
    }

    protected void initNet() {
        ProgressBarUtil.showProgress(mContext);
        mAdapter = initAdapter();
        list_recy.setAdapter(mAdapter);
        addHeaderData(mAdapter);

        initNet(false);
    }

    /**
     * 添加头部
     * 满足业务拓展需要
     */
    public void addHeaderData(BaseQuickAdapter mAdapter) {

    }

    public void initNet(final boolean isLoadMore) {
        mGetReq = new OKHttpGetReq(this, getUrl());

        mGetReq.getDataWithParam(new Callback() {
            @Override
            public void onError(String resp) {
                ProgressBarUtil.dismissProgress(mContext);
                AdapterLoadDataBusiness.finishLoadAndRefresh(list_swipe);

                AdapterLoadDataBusiness.LoadErrorData(mContext, initAdapter(), list_recy, new AdapterLoadDataBusiness.ReLoadNet() {
                    @Override
                    public void retryLoadNet() {
                        initNet(false);
                    }
                });
            }

            @Override
            public void onResponse(String resp, int id) {
                ProgressBarUtil.dismissProgress(mContext);

                AdapterLoadDataBusiness.finishLoadAndRefresh(list_swipe);
                processData(resp, isLoadMore);
            }
        }, initHashMap());
    }

    public void processData(String resp, boolean isLoadMore) {
        try {
            mPOJO = parseData(resp);
        } catch (Exception e) {
            mPOJO = null;
        }
        View empty = LayoutInflater.from(mContext).inflate(R.layout.empty_data, null);
        mAdapter.setHeaderFooterEmpty(true, true);
        mAdapter.setEmptyView(empty);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                setAdapterClick(adapter, view, position);
            }
        });

        if (isLoadMore && mPOJO != null) {
            adapterAddData();
        } else if (mPOJO != null) {
            adapterSetData();
        }
    }

    /**
     * 处理页面中的点击事件
     *
     * @param adapter
     * @param view
     * @param position
     */
    public void setAdapterClick(BaseQuickAdapter adapter, View view, int position) {

    }


    /**
     * @return 实例化adapter
     */
    public abstract BaseQuickAdapter initAdapter();

    public abstract String getUrl();

    public abstract HashMap<String, String> initHashMap();

    /**
     * @return 实例化 Bean数据
     */
    public abstract T parseData(String resp);

    /**
     * 初始化以及下拉刷新设置数据
     */
    protected abstract void adapterSetData();

    /**
     * 加载更多数据
     */
    protected abstract void adapterAddData();


}
